import { Effect, Reducer } from 'umi';
import { _login } from '@/api/login';
import { history } from 'umi';
import { message } from 'antd';
export interface LoginModelState {}

export interface LoginModelType {
  namespace: 'login';
  state: LoginModelState;
  effects: {
    LOGIN: Effect;
  };
  reducers: {};
}
const LoginModel: LoginModelType = {
  namespace: 'login',
  state: {},
  effects: {
    //调用登录接口
    *LOGIN({ payload }, { call }) {
      console.log(payload, 'akdjaskldjkl');
      try {
        let res = yield call(_login, payload);
        //登陆成功
        if (res.data.code == 1) {
          localStorage.token = res.data.token;
          localStorage.user_id = res.data.userInfo.user_id;
          history.push('/index');
        } else {
          //登录失败
          message.error(res.data.msg);
        }
      } catch (error) {
        message.error("该用户不存在");
      }
    },
  },
  reducers: {},
};

export default LoginModel;
